package com.springsecurity.comment;

import com.springsecurity.constants.MessageConstant;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <h3>Spring-Security</h3>
 * <p>异常处理类</p>
 *
 * @author gdw
 * @since 2024-07-11 09:37:17
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})//只要类上加了这两个处理器的就会被拦截
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    public R handleAccessDeniedException(AccessDeniedException ex) {
        return R.error(MessageConstant.USER_ROLE_ERROR);
    }
    @ExceptionHandler({RuntimeException.class})
    public R runtimeException(RuntimeException ex) {
        String message = ex.getMessage();
        return R.error(message);
    }

}
